import cn.yunhe.entity.Person;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Demo {
Session session=null;
Transaction tr=null;
@Before
    public void init(){
        Configuration configuration=new Configuration().configure();
        SessionFactory sessionFactory=configuration.buildSessionFactory();
        session=sessionFactory.openSession();
        tr=session.beginTransaction();

    }
@Test
/*
增加
 */
    public void test1(){
        Person person=new Person("章程","123",13);
        session.save(person);
    }
    @Test
    /*
查询
 */
    public void test2(){
      Person person= (Person) session.get(Person.class,3);
        System.out.println(person.getName()+person.getAge());

    }


@Test
    /*
修改
 */
    public void test3(){
      Person person= (Person) session.get(Person.class,2);
      person.setName("李二");
      session.update(person);

    }

    @Test
    /*
    删除1
     */
  public void test4(){
Person person= (Person) session.get(Person.class,3);
session.delete(person);
  }

@Test
    /*
        删除2
         */
  public void test5(){
      session.delete(getPersonById(2));
  }
public Person getPersonById(int oid){
      Person p= (Person) session.get(Person.class,2);
  return p;
  }

@Test
/*
        创建（不带id的时候）  或 修改(id表中有)
         */
  public void test6(){

      Person person=new Person(1,"zhu","pwd",23);
      session.saveOrUpdate(person);
  }

@After
    public void des(){
        tr.commit();
        session.close();
    }


}
